const fs = require('fs')
const url = require('url')
const path = require('path')
const {chromium} = require('playwright')
const cheerio = require('cheerio')
const axios = require('axios')
const uuid4 = require('uuid4')

const target_url = 'https://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gb18030&word=%B6%AF%C2%FE%CD%B7%CF%F1&fr=ala&ala=1&alatpl=head&pos=0'

async function saveFile(file_url){
    var parsed = url.parse(file_url);
    var filename = path.resolve("./out",uuid4().toString()+".png");

    if(!fs.existsSync(filename)){
        const res = await axios.get(file_url,{ responseType : 'arrayBuffer'});
        fs.writeFileSync(filename,new Buffer.from(res.data),'binary');
        console.log("->",filename)
    }
}

async function run(){
    const browser = await chromium.launch({headless: false,slowMo: 100});
    //页面访问
    const page = await  browser.newPage();
    await page.goto(target_url);
    await page.waitForTimeout(3000);
    await page.evaluate(()=>{
        window.scrollTo(0,document.body.scrollHeight)
    });
    await page.waitForTimeout(3000);
    //获取页面html
    var content = await page.content();
    //解析出img图片
    const $ = cheerio.load(content);
    $('img').each(async (i,elem)=>{
        var image_src = $(elem).attr('src');
        if(image_src){
            await saveImg(image_src);
        }
    })

    await browser.close();
    console.log('finish...')
}

async function saveImg(file_src){
 axios.get(file_src, {
    responseType: 'arraybuffer'
   })
    .then(function(response) {
       // console.log(response.status);
       // console.log(response.headers);
        fs.writeFile("./cartoon/"+uuid4().toString()+".jpg", response.data, function (err) {
          if (err) throw err;
          console.log('保存成功');
       });
   });
}



run()